package com.wu.foodsafetyplayer;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.wu.foodsafetyplayer.adapter.MainNvaAdapter;
import com.wu.foodsafetyplayer.adapter.MainUrlAdapter;
import com.wu.foodsafetyplayer.adapter.MainVideoViewAdapter;
import com.wu.foodsafetyplayer.widget.ImgPlayerViewFlipper;

import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity implements AdapterView.OnItemClickListener {
    private static final String TAG = MainActivity.class.getSimpleName();
    private GridView mVideoView;
    private int width, height;
    private GridView mNvaView;
    private TextView rightTitle, rightTitleEn, company;
    private TextView grade1, grade2;
    private ImageView gradeImg1, gradeImg2;

    private ImgPlayerViewFlipper imgPlayerViewFlipper;
    private ListView listView;

    private List<String> imgPlayer = new ArrayList<>();

    private String[] itemUrls = new String[]{"", "", "rtsp://10.9.0.102:554/user=admin&password=&channel=0&stream=0.sdp?", "rtsp://10.9.0.155:554/user=admin&password=&channel=0&stream=1.sdp?", "rtsp://10.9.0.106:554/user=admin&password=abc-123&channel=0&stream=0.sdp?", "rtsp://10.9.0.154:554/user=admin&password=&channel=0&stream=0.sdp?"};

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();
    }

    public void init() {
        //播放控件
        mVideoView = findViewById(R.id.main_player);
        mVideoView.setClickable(false);
        mVideoView.setFocusable(false);
        mVideoView.setEnabled(false);
        mVideoView.postDelayed(new Runnable() {
            @Override
            public void run() {
                width = mVideoView.getWidth();
                height = mVideoView.getHeight();
                Log.e(TAG, width + "------" + height);
                setmVideoViewAdapter(2);
            }
        }, 1000);


        //摄像头列表
        listView = findViewById(R.id.main_list);
        listView.setOnItemClickListener(this);
        MainUrlAdapter mainUrlAdapter = new MainUrlAdapter(this);
        listView.setAdapter(mainUrlAdapter);

        //底部导航
        mNvaView = findViewById(R.id.main_nva_gv);
        mNvaView.setOnItemClickListener(this);
        MainNvaAdapter mainNvaAdapter = new MainNvaAdapter(this);
        mNvaView.setAdapter(mainNvaAdapter);

        rightTitle = findViewById(R.id.main_right_title);
        rightTitleEn = findViewById(R.id.main_right_title_en);
        company = findViewById(R.id.main_company);

        grade1 = findViewById(R.id.main_grade_name1);
        gradeImg1 = findViewById(R.id.main_grade_img1);
        grade2 = findViewById(R.id.main_grade_name2);
        gradeImg2 = findViewById(R.id.main_grade_img2);

        imgPlayerViewFlipper = findViewById(R.id.main_img_player);
        imgPlayer.add("https://i01piccdn.sogoucdn.com/2d66095f061d1b36");
        imgPlayer.add("https://i04piccdn.sogoucdn.com/c4cec9551618c56d");
        imgPlayer.add("https://i02piccdn.sogoucdn.com/147b46fe2fc68d29");
        imgPlayerViewFlipper.initImg(imgPlayer);
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        switch (adapterView.getId()) {
            case R.id.main_list:
                setmVideoViewAdapter(i);
                break;
            case R.id.main_nva_gv:

                break;
        }
    }

    /**
     * 设置播放控件
     *
     * @param i
     */
    public void setmVideoViewAdapter(int i) {
        if (i == 0 || i == 1) {
            mVideoView.setNumColumns(2);
            if (i == 0) {
                i = 2;
            } else {
                i = 4;
            }
            MainVideoViewAdapter mainVideoViewAdapter = new MainVideoViewAdapter(MainActivity.this, width, height, i, null);
            mVideoView.setAdapter(mainVideoViewAdapter);
        } else {
            mVideoView.setNumColumns(1);
            MainVideoViewAdapter mainVideoViewAdapter = new MainVideoViewAdapter(MainActivity.this, width, height, 0, itemUrls[i]);
            mVideoView.setAdapter(mainVideoViewAdapter);
        }
    }
}
